package com.hongyuan.coach.myview.select_time;

import android.app.Dialog;
import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.hongyuan.mvvmhabitx.utils.ToastUtils;
import com.hongyuan.coach.R;
import com.hongyuan.coach.base.CustomViewModel;
import com.hongyuan.coach.data.Constants;
import com.hongyuan.coach.data.md5.BaseUtil;
import com.hongyuan.coach.ui.home.activity.EditTimeActivity;
import com.hongyuan.coach.ui.home.beans.TopTimeBean;
import com.hongyuan.coach.utils.DividerItemDecoration;
import com.hongyuan.coach.utils.TimeUtil;
import java.util.List;

public class SelectTimeView extends LinearLayout implements View.OnClickListener {

    private RelativeLayout selectBox;
    private Dialog dialog;
    private RecyclerView tabRecyc,contentRecyc;
    private TabTimeAdapter tabAdapter;
    private TimeAdapter timeAdapter;
    private CustomViewModel viewModel;
    private TextView showTimeText;
    private String jl_mid;

    private String startTime = "";
    private String endTime = "";

    //头部时间数据
    private List<TopTimeBean> tabTimeBean;
    //空闲时间
    private List<ContentTimeBean> timeBean;

    public SelectTimeView(Context context) {
        super(context);
        initLayoutView();
    }

    public SelectTimeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initLayoutView();
    }

    public SelectTimeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initLayoutView();
    }

    public void initLayoutView(){
        View view = View.inflate(getContext(), R.layout.view_select_time, this);
        selectBox = view.findViewById(R.id.selectBox);
        showTimeText = view.findViewById(R.id.showTimeText);

        selectBox.setOnClickListener(this);
    }


    //确定收款对话框
    public void showTimes(Context mContext){
        if(dialog == null){
            dialog = new Dialog(mContext, R.style.DialogTheme);
            View view = View.inflate(mContext, R.layout.dialog_select_time,null);
            dialog.setContentView(view);
            Window window = dialog.getWindow();
            window.setGravity(Gravity.BOTTOM);
            window.setWindowAnimations(R.style.bottom_in_out);
            window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);

            tabRecyc = view.findViewById(R.id.tabRecyc);
            contentRecyc = view.findViewById(R.id.contentRecyc);

            LinearLayoutManager tabManager = new LinearLayoutManager(getContext());
            tabManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            tabRecyc.addItemDecoration(new DividerItemDecoration(
                    getContext(), DividerItemDecoration.VERTICAL_LIST,100,0x00000000));
            tabRecyc.setLayoutManager(tabManager);
            tabAdapter = new TabTimeAdapter();
            tabRecyc.setAdapter(tabAdapter);


            GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 4);
            contentRecyc.setLayoutManager(layoutManager);
            contentRecyc.setLayoutManager(layoutManager);
            timeAdapter = new TimeAdapter();
            contentRecyc.setAdapter(timeAdapter);

            tabAdapter.setOnItemChildClickListener((adapter, view1, position) -> {
                changeTab(position);
                reqTimes(tabTimeBean.get(position).getNow_day());
            });

            timeAdapter.setOnItemChildClickListener((adapter, view12, position) -> changeTime(position));

            TextView submit = view.findViewById(R.id.submit);
            view.findViewById(R.id.goEdit).setOnClickListener(view1 -> {
                viewModel.startActivity(EditTimeActivity.class);
            });

            submit.setOnClickListener(v -> {
                if(getSelectTime().length() < 13){
                    ToastUtils.showShort("请选择时间");
                }else{
                    showTimeText.setText(getSelectTime());
                    dialog.dismiss();
                }

            });
        }

        try {
            tabAdapter.setNewData(tabTimeBean);
            timeAdapter.setNewData(timeBean);
            dialog.show();
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    /*
    * 更改头部时间选中状态
    * */
    private void changeTab(int position){
        for(int i = 0 ; i < tabTimeBean.size() ; i++){
            if(i == position){
                tabTimeBean.get(i).setSelect(true);
            }else{
                tabTimeBean.get(i).setSelect(false);
            }
        }
        tabAdapter.setNewData(tabTimeBean);
    }

    /*
    * 更改时间段的选中状态
    * */
    private void changeTime(int position){
        for(int i = 0 ; i < timeBean.size() ; i++){
            if(i == position){
                timeBean.get(i).setSelect(true);
            }else{
                timeBean.get(i).setSelect(false);
            }
        }
        timeAdapter.setNewData(timeBean);
    }

    /*
    * 获取当前选择的时间
    * */
    private String getSelectTime(){
        String showTime = "";
        startTime = "";
        endTime = "";
        for(TopTimeBean tabTime : tabTimeBean){
            if(tabTime.isSelect()){
                showTime += tabTime.getNow_day();

                //给起始时间赋值
                startTime += tabTime.getNow_day();
                //给结束时间赋值
                endTime += tabTime.getNow_day();
            }
        }

        for(int i = 0 ; i < timeBean.size() ; i++){
            if(timeBean.get(i).isSelect()){
                showTime += " " + timeBean.get(i).getNow_time();
                showTime = TimeUtil.getStringByFormat(showTime,TimeUtil.dateFormat);
                showTime +="-"+ timeBean.get(i+2).getNow_time().substring(0,5);

                //给起始时间赋值
                startTime += " " + timeBean.get(i).getNow_time();
                //给结束时间赋值
                endTime += " " + timeBean.get(i+2).getNow_time();
                break;
            }
        }
        return showTime;
    }

    /*
    * 获取选中的初始时间
    * */
    public String getStartTime() {
        return startTime;
    }

    /*
    * 获取选中的结束时间
    * */
    public String getEndTime() {
        return endTime;
    }


    @Override
    public void onClick(View v) {
        if(!BaseUtil.isValue(jl_mid)){
            ToastUtils.showShort("请先选择教练！");
            return;
        }
        showTimes(getContext());
    }

    /*
    * 设置viewmodel
    * */
    public void setViewModel(CustomViewModel viewModel){
        this.viewModel = viewModel;
    }

    /*
    * 设置头部数据
    * */
    public void setTopData(String jl_mid,List<TopTimeBean> tabTimeBean){
        this.jl_mid = jl_mid;
        this.tabTimeBean = tabTimeBean;

        tabTimeBean.get(getTodayPosition(tabTimeBean)).setSelect(true);
        //请求空闲时间
        reqTimes(tabTimeBean.get(getTodayPosition(tabTimeBean)).getNow_day());
        if(tabAdapter != null){
            tabAdapter.setNewData(tabTimeBean);
        }
    }

    /*
    * 设置空闲时间的显示
    * */
    public void setContentData(List<ContentTimeBean> timeBean){
        this.timeBean = timeBean;

        if(timeAdapter != null){
            timeAdapter.setNewData(timeBean);
        }
    }

    /*
    * 清空显示的时间内容
    * */
    public void clearData(){
        if(tabAdapter != null && timeAdapter != null){
            tabAdapter.getData().clear();
            timeAdapter.getData().clear();
            tabAdapter.notifyDataSetChanged();
            timeAdapter.notifyDataSetChanged();

            showTimeText.setText("请选择");
            jl_mid = "";
            startTime = "";
        }
    }

    /*
     * 请求教练时间列表
     * */
    private void reqTimes(String days){

        viewModel.setParams("day",days).setParams("jl_mid",jl_mid)
        .requestData(Constants.GET_COACH_TIMEPLAN_DAY_LIST);
    }

    /*
    * 查询今日时间位置
    * */
    private int getTodayPosition(List<TopTimeBean> mList){
        for(int i = 0 ; i < mList.size() ; i++){
            if(mList.get(i).getIs_cur_date() == 1){
                return i;
            }
        }
        return 0;
    }
}
